Building PICT 2
Volume Number: 10
Issue Number: 3
Column Tag: Getting Started
Related Info: List Manager Resource Manager
Using The List Manager
Building and using a list of PICT resources
By Dave Mark, MacTech Magazine Regular Contributing Author
Note: Source code files accompanying article are located on MacTech CD-ROM or
source code disks.
Last month’s column presented PictLister, a program designed to showcase the
Mac Toolbox’s List Manager. The vast majority of Macintosh applications make use of
the List Manager, albeit indirectly. Figure 1 shows a call to StandardGetFile(), the
Mac Toolbox’s standard mechanism for selecting a file to open. The scrolling list in
Figure 1 was implemented by the List Manager.
Figure 1. The List Manager, as used by StandardGetFile().
Just as a reminder, we’ll put PictLister through its paces before we walk
through the source code. Startup THINK C by double-clicking on the file PictLister.π.
When the project opens, select Run from the Project menu.
PictLister features three menus: Apple, File, and Edit. Figure 2 shows the File
menu.
Figure 2. PictLister’s File menu.
Close closes the frontmost window and Quit quits PictLister. New List builds a list
out of all available PICT resources, then creates a window to display the list. It’s
important to note that the Resource Manager searches all open resource files in its
quest for a particular resource type. At the very least, this search includes the
application’s resource fork as well as the System file in the currently blessed System
Folder (a.k.a., the System on the startup disk).
Figure 3 shows a sample PictLister window.
Figure 3. A PictLister window.
The entire content region of the window (including both scroll bars, but not the
window’s title bar) is dedicated to the window’s list. With very little effort on our
part (just a call here or there) the List Manager will handle the scroll bars, clicks in
the list, auto-scrolling (click in the bottom of the list and drag down), update events,
etc. As you’ll see, the List Manager gives you a lot of functionality with very little
work on your part.
Once the PictLister window appears, you can do all the normal window-type
things. You can drag the window, resize it, and close it by clicking in the close box.
If you click on a name in the list, the List Manager will highlight the name. Click
on another name, the first name will be unhighlighted, then that name will be
highlighted. If you double-click on a name, a new window will appear showing the
specified PICT.
By the way, the names in the list are drawn directly from the names of the
associated PICT resource. If the resource isn’t named, we use the string “Unnamed” to
name the string.
Walking Through the Source Code
PictLister starts off with a bunch of #defines, some familiar, some not. As usual,
you’ll see what they do in context.
/* 1 */
#define kMBARResID 128
#define kSleep 60L
#define kMoveToFront (WindowPtr)-1L
#define kNilFilterProc (ProcPtr)0L
#define kEmptyString "\p
#define kHasGoAway true
#define kInvisible false
#define kListDefProc 0
#define kDontDrawYet false
#define kHasGrow true
#define kHasHScroll true
#define kHasVScroll true
#define kFindNext true
#define kListWindow 0
#define kDAWindow 1
#define kUnknownWindow 2
#define kPictWindow 3
#define kNilWindow 4
#define kMinWindowWidth 210
#define kMinWindowHeight 63
#define kWindowHeight 255
#define kMinPictWinHeight 50
#define kMinPictWinWidth 150
#define mApple 128
#define iAbout 1
#define mFile 129
#define iNewList 1
#define iClose 2
#define iQuit 4
#define kErrorAlertID 128
Frequently, you’ll want to attach additional information to a window. Suppose you
wrote a program that implemented a personal phone book. Suppose your program
creates an individual window for each person in the phone book. Each window would
have the same fields but would contain different data to place in the fields when the
window was updated.
One way to write this program is to create a struct containing the data for each
window, allocate memory for the struct when you create the window, then tie it to the
window. When it comes time to update the window, retrieve the struct tied to that
window and use the data in the struct to fill in the window’s fields. This technique is
known as window piggybacking. You’ll see how this works as we walk through the
code.!cuses the piggybacking technique to tie the list to the list window and to tie the
PICT to the PICT window. This is done by embedding a WindowRecord in each of the
following typedefs.
/* 2 */
/************************/
/* Typedefs */
/************************/
typedef struct
{
WindowRecord w;
short wType;
ListHandle list;
} ListRecord, *ListPeek;
typedef struct
{
WindowRecord w;
short wType;
short PictResID;
} PictRecord, *PictPeek;